package com.my.javaweb.datetime;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;

/**
 * @author Felix
 * @date 2023/11/17 21:52
 */
public class DateTimeUtils {
    /*
    工作日    自然日    小时    分钟
    大于    小于    等于
    */

    public static String getDateStr(Integer number, String timeUnit, String operator) {

        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        LocalDateTime result;
        switch (timeUnit) {
            case "工作日" :
                result = getWorkDateTime2(now, number);
                break;
            case "自然日" :
                result = now.minusDays(number);
                break;
            case "小时" :
                result = now.minus(number, ChronoUnit.HOURS);
                break;
            case "分钟" :
                result = now.minus(number, ChronoUnit.MINUTES);
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + timeUnit);
        }
        return result.format(formatter);
    }

    public static LocalDateTime getWorkDateTime(LocalDateTime currentDate, Integer n) {
        while (n > 0) {
            currentDate = currentDate.minusDays(1);
            if (currentDate.getDayOfWeek() != DayOfWeek.SATURDAY &&
                    currentDate.getDayOfWeek() != DayOfWeek.SUNDAY) {
                n--;
            }
        }
        return currentDate;
    }

    public static LocalDateTime getWorkDateTime2(LocalDateTime currentDate, Integer number) {
        int n = number % 5;
        int m = number / 5;
        currentDate = currentDate.minusDays(m * 7);

        while (n > 0) {
            currentDate = currentDate.minusDays(1);
            if (currentDate.getDayOfWeek() != DayOfWeek.SATURDAY &&
                    currentDate.getDayOfWeek() != DayOfWeek.SUNDAY) {
                n--;
            }
        }
        return currentDate;
    }

    public static void main(String[] args) {
        System.out.println("自然日\t" + getDateStr(1, "自然日", "bt"));
        System.out.println("1工作日\t" + getDateStr(1, "工作日", "bt"));
        System.out.println("2工作日\t" + getDateStr(2, "工作日", "bt"));
        System.out.println("3工作日\t" + getDateStr(3, "工作日", "bt"));
        System.out.println("4工作日\t" + getDateStr(4, "工作日", "bt"));
        System.out.println("5工作日\t" + getDateStr(5, "工作日", "bt"));
        System.out.println("6工作日\t" + getDateStr(6, "工作日", "bt"));
        System.out.println("7工作日\t" + getDateStr(7, "工作日", "bt"));
        System.out.println("8工作日\t" + getDateStr(8, "工作日", "bt"));
        System.out.println("小时\t" + getDateStr(28, "小时", "bt"));
        System.out.println("分钟\t" + getDateStr(35, "分钟", "bt"));
    }
}
